<!DOCTYPE html>
<html lang="zh" dir="ltr" class="client-nojs">
<head>
<title>算术运算符</title>
<meta charset="UTF-8">
<meta name="generator" content="MediaWiki 1.21.2">
<link rel="shortcut icon" href="../../../common/favicon.ico">
<link rel="stylesheet" href="../../../common/ext.css">
<meta name="ResourceLoaderDynamicStyles" content="">
<link rel="stylesheet" href="../../../common/site_modules.css">
<style>a:lang(ar),a:lang(ckb),a:lang(fa),a:lang(kk-arab),a:lang(mzn),a:lang(ps),a:lang(ur){text-decoration:none}#toc{display:none}.editsection{display:none}
/* cache key: mwiki1-mwiki_zh_:resourceloader:filter:minify-css:7:15cea3ec788a65b5187d4018eed543bf */</style>

<script src="../../../common/startup_scripts.js"></script>
<script>if(window.mw){
mw.config.set({"wgCanonicalNamespace":"","wgCanonicalSpecialPageName":false,"wgNamespaceNumber":0,"wgPageName":"c/language/operator_arithmetic","wgTitle":"c/language/operator arithmetic","wgCurRevisionId":63501,"wgArticleId":7985,"wgIsArticle":true,"wgAction":"view","wgUserName":null,"wgUserGroups":["*"],"wgCategories":["Todo with reason"],"wgBreakFrames":false,"wgPageContentLanguage":"zh","wgSeparatorTransformTable":["",""],"wgDigitTransformTable":["",""],"wgDefaultDateFormat":"zh","wgMonthNames":["","1月","2月","3月","4月","5月","6月","7月","8月","9月","10月","11月","12月"],"wgMonthNamesShort":["","1月","2月","3月","4月","5月","6月","7月","8月","9月","10月","11月","12月"],"wgRelevantPageName":"c/language/operator_arithmetic","wgUserVariant":"zh","wgRestrictionEdit":[],"wgRestrictionMove":[]});
}</script><script>if(window.mw){
mw.loader.implement("user.options",function(){mw.user.options.set({"ccmeonemails":0,"cols":80,"date":"default","diffonly":0,"disablemail":0,"disablesuggest":0,"editfont":"default","editondblclick":0,"editsection":0,"editsectiononrightclick":0,"enotifminoredits":0,"enotifrevealaddr":0,"enotifusertalkpages":1,"enotifwatchlistpages":0,"extendwatchlist":0,"externaldiff":0,"externaleditor":0,"fancysig":0,"forceeditsummary":0,"gender":"unknown","hideminor":0,"hidepatrolled":0,"imagesize":2,"justify":0,"math":1,"minordefault":0,"newpageshidepatrolled":0,"nocache":0,"noconvertlink":0,"norollbackdiff":0,"numberheadings":0,"previewonfirst":0,"previewontop":1,"quickbar":5,"rcdays":7,"rclimit":50,"rememberpassword":0,"rows":25,"searchlimit":20,"showhiddencats":0,"showjumplinks":1,"shownumberswatching":1,"showtoc":0,"showtoolbar":1,"skin":"cppreference2","stubthreshold":0,"thumbsize":2,"underline":2,"uselivepreview":0,"usenewrc":0,"watchcreations":0,"watchdefault":0,"watchdeletion":0,
"watchlistdays":3,"watchlisthideanons":0,"watchlisthidebots":0,"watchlisthideliu":0,"watchlisthideminor":0,"watchlisthideown":0,"watchlisthidepatrolled":0,"watchmoves":0,"wllimit":250,"variant":"zh","language":"zh","searchNs0":true,"searchNs1":false,"searchNs2":false,"searchNs3":false,"searchNs4":false,"searchNs5":false,"searchNs6":false,"searchNs7":false,"searchNs8":false,"searchNs9":false,"searchNs10":false,"searchNs11":false,"searchNs12":false,"searchNs13":false,"searchNs14":false,"searchNs15":false,"gadget-MathJax":1,"gadget-ColiruCompiler":1});;},{},{});mw.loader.implement("user.tokens",function(){mw.user.tokens.set({"editToken":"+\\","patrolToken":false,"watchToken":false});;},{},{});
/* cache key: mwiki1-mwiki_zh_:resourceloader:filter:minify-js:7:258d7cd6aa9aa67dee25e01fb6a9e505 */
}</script>
<script>if(window.mw){
mw.loader.load(["mediawiki.page.startup","mediawiki.legacy.wikibits","mediawiki.legacy.ajax"]);
}</script>
<style type="text/css">/*<![CDATA[*/
.source-c {line-height: normal;}
.source-c li, .source-c pre {
	line-height: normal; border: 0px none white;
}
/**
 * GeSHi Dynamically Generated Stylesheet
 * --------------------------------------
 * Dynamically generated stylesheet for c
 * CSS class: source-c, CSS id: 
 * GeSHi (C) 2004 - 2007 Nigel McNie, 2007 - 2008 Benny Baumann
 * (http://qbnz.com/highlighter/ and http://geshi.org/)
 * --------------------------------------
 */
.c.source-c .de1, .c.source-c .de2 {font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;}
.c.source-c  {font-family:monospace;}
.c.source-c .imp {font-weight: bold; color: red;}
.c.source-c li, .c.source-c .li1 {font-weight: normal; vertical-align:top;}
.c.source-c .ln {width:1px;text-align:right;margin:0;padding:0 2px;vertical-align:top;}
.c.source-c .li2 {font-weight: bold; vertical-align:top;}
.c.source-c .kw1 {color: #0000dd;}
.c.source-c .kw2 {color: #0000ff;}
.c.source-c .kw3 {color: #0000dd;}
.c.source-c .kw4 {color: #0000ff;}
.c.source-c .co1 {color: #909090;}
.c.source-c .co2 {color: #339900;}
.c.source-c .coMULTI {color: #ff0000; font-style: italic;}
.c.source-c .es0 {color: #008000; font-weight: bold;}
.c.source-c .es1 {color: #008000; font-weight: bold;}
.c.source-c .es2 {color: #008000; font-weight: bold;}
.c.source-c .es3 {color: #008000; font-weight: bold;}
.c.source-c .es4 {color: #008000; font-weight: bold;}
.c.source-c .es5 {color: #008000; font-weight: bold;}
.c.source-c .br0 {color: #008000;}
.c.source-c .sy0 {color: #008000;}
.c.source-c .sy1 {color: #000080;}
.c.source-c .sy2 {color: #000040;}
.c.source-c .sy3 {color: #000040;}
.c.source-c .sy4 {color: #008080;}
.c.source-c .st0 {color: #008000;}
.c.source-c .nu0 {color: #000080;}
.c.source-c .nu6 {color:#000080;}
.c.source-c .nu8 {color:#000080;}
.c.source-c .nu12 {color:#000080;}
.c.source-c .nu16 {color:#000080;}
.c.source-c .nu17 {color:#000080;}
.c.source-c .nu18 {color:#000080;}
.c.source-c .nu19 {color:#000080;}
.c.source-c .ln-xtra, .c.source-c li.ln-xtra, .c.source-c div.ln-xtra {background-color: #ffc;}
.c.source-c span.xtra { display:block; }

/*]]>*/
</style><style type="text/css">/*<![CDATA[*/
.source-text {line-height: normal;}
.source-text li, .source-text pre {
	line-height: normal; border: 0px none white;
}
/**
 * GeSHi Dynamically Generated Stylesheet
 * --------------------------------------
 * Dynamically generated stylesheet for text
 * CSS class: source-text, CSS id: 
 * GeSHi (C) 2004 - 2007 Nigel McNie, 2007 - 2008 Benny Baumann
 * (http://qbnz.com/highlighter/ and http://geshi.org/)
 * --------------------------------------
 */
.text.source-text .de1, .text.source-text .de2 {font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;}
.text.source-text  {font-family:monospace;}
.text.source-text .imp {font-weight: bold; color: red;}
.text.source-text li, .text.source-text .li1 {font-weight: normal; vertical-align:top;}
.text.source-text .ln {width:1px;text-align:right;margin:0;padding:0 2px;vertical-align:top;}
.text.source-text .li2 {font-weight: bold; vertical-align:top;}
.text.source-text .ln-xtra, .text.source-text li.ln-xtra, .text.source-text div.ln-xtra {background-color: #ffc;}
.text.source-text span.xtra { display:block; }

/*]]>*/
</style><!--[if lt IE 7]><style type="text/css">body{behavior:url("/mwiki/skins/cppreference2/csshover.min.htc")}</style><![endif]--></head>
<body class="mediawiki ltr sitedir-ltr ns-0 ns-subject page-c_language_operator_arithmetic skin-cppreference2 action-view cpp-navbar">
        <!-- header -->
        <!-- /header -->
        <!-- content -->
<div id="cpp-content-base">
            <div id="content">
                <a id="top"></a>
                <div id="mw-js-message" style="display:none;"></div>
                                <!-- firstHeading -->
<h1 id="firstHeading" class="firstHeading">算术运算符</h1>
                <!-- /firstHeading -->
                <!-- bodyContent -->
                <div id="bodyContent">
                                        <!-- tagline -->
                    <div id="siteSub">来自cppreference.com</div>
                    <!-- /tagline -->
                                        <!-- subtitle -->
                    <div id="contentSub"><span class="subpages">&lt; <a href="../../c.html" title="c">c</a>‎ | <a href="../language.html" title="c/language">language</a></span></div>
                    <!-- /subtitle -->
                                                            <!-- bodycontent -->
                    <div id="mw-content-text" lang="zh" dir="ltr" class="mw-content-ltr"><div class="t-navbar" style=""><div class="t-navbar-sep"> </div><div class="t-navbar-head"><a href="../../c.html" title="c"> C</a><div class="t-navbar-menu"><div><div><table class="t-nv-begin" cellpadding="0" style="line-height:1.1em;">
<tr class="t-nv"><td colspan="5"> <a href="../language.html" title="c/language">语言</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../header.html" title="c/header">头文件</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../types.html" title="c/types">类型支持</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../program.html" title="c/program">程序工具</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../variadic.html" title="c/variadic">变参数函数支持</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../memory.html" title="c/memory">动态内存管理</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../error.html" title="c/error">错误处理</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../chrono.html" title="c/chrono">日期和时间工具</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../string.html" title="c/string">字符串库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../algorithm.html" title="c/algorithm">算法</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../numeric.html" title="c/numeric">数值</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../io.html" title="c/io">输入/输出支持</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../locale.html" title="c/locale">本地化支持</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../thread.html" title="c/thread">线程支持</a> <span class="t-mark-rev t-since-c11">(C11)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../atomic.html" title="c/atomic">原子操作</a> <span class="t-mark-rev t-since-c11">(C11)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../experimental.html" title="c/experimental">技术规范</a> </td></tr>
</table></div><div></div></div></div></div><div class="t-navbar-sep"> </div><div class="t-navbar-head"><a href="../language.html" title="c/language">C 语言</a><div class="t-navbar-menu"><div><div><table class="t-nv-begin" cellpadding="0" style="">
<tr class="t-nv-h2"><td colspan="5"> <a href="basic_concepts.html" title="c/language/basic concepts">基本概念</a></td></tr>
<tr class="t-nv-h2"><td colspan="5"> <a href="../keyword.html" title="c/keyword">关键词</a></td></tr>
<tr class="t-nv-h2"><td colspan="5"> <a href="../preprocessor.html" title="c/preprocessor">预处理器</a></td></tr>
<tr class="t-nv-h2"><td colspan="5"> <a href="statements.html" title="c/language/statements">语句</a></td></tr>
<tr class="t-nv-h2"><td colspan="5"> <a href="expressions.html" title="c/language/expressions">表达式</a></td></tr>
<tr class="t-nv-h2"><td colspan="5"> <a href="initialization.html" title="c/language/initialization">初始化</a></td></tr>
<tr class="t-nv-h2"><td colspan="5"> <a href="declarations.html" title="c/language/declarations">声明</a></td></tr>
<tr class="t-nv-h2"><td colspan="5"> <a href="functions.html" title="c/language/functions">函数</a></td></tr>
<tr class="t-nv"><td colspan="5"> 杂项</td></tr>
<tr class="t-nv-h2"><td colspan="5"> <a href="history.html" title="c/language/history">C 的历史</a></td></tr>
<tr class="t-nv-h2"><td colspan="5"> <a href="../experimental.html" title="c/experimental">技术规范</a> </td></tr>
</table></div><div></div></div></div></div><div class="t-navbar-sep"> </div><div class="t-navbar-head"><a href="expressions.html" title="c/language/expressions"> 表达式</a><div class="t-navbar-menu"><div><div style="display:inline-block">
<div><table class="t-nv-begin" cellpadding="0" style="">
<tr class="t-nv-h2"><td colspan="5"> 通用 </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="value_category.html" title="c/language/value category">值类别</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="eval_order.html" title="c/language/eval order"> 求值顺序与顺序点</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="constant_expression.html" title="c/language/constant expression">常量表达式</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="conversion.html" title="c/language/conversion">隐式转换</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="generic.html" title="c/language/generic">泛型选择</a></td></tr>
<tr class="t-nv-h2"><td colspan="5"> <a href="expressions.html#.E5.B8.B8.E9.87.8F.E5.8F.8A.E5.AD.97.E9.9D.A2.E9.87.8F" title="c/language/expressions">常量及字面量</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="integer_constant.html" title="c/language/integer constant">整数常量</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="floating_constant.html" title="c/language/floating constant">浮点常量</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="character_constant.html" title="c/language/character constant">字符常量</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="string_literal.html" title="c/language/string literal">字符串字面量</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="compound_literal.html" title="c/language/compound literal">复合字面量</a></td></tr>
<tr class="t-nv-h2"><td colspan="5"> <a href="expressions.html#.E8.BF.90.E7.AE.97.E7.AC.A6" title="c/language/expressions">运算符</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="operator_precedence.html" title="c/language/operator precedence">运算符优先级</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="operator_member_access.html" title="c/language/operator member access">成员访问与间接</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="operator_logical.html" title="c/language/operator logical">逻辑运算符</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="operator_comparison.html" title="c/language/operator comparison">比较运算符</a></td></tr>
<tr class="t-nv"><td colspan="5"> <strong class="selflink">算术运算符</strong></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="operator_assignment.html" title="c/language/operator assignment">赋值运算符</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="operator_incdec.html" title="c/language/operator incdec">自增减</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="operator_other.html" title="c/language/operator other">函数调用、逗号、条件运算符</a></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="sizeof.html" title="c/language/sizeof"><span class="t-lines"><span>sizeof</span></span></a></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="_Alignof.html" title="c/language/ Alignof"><span class="t-lines"><span>_Alignof</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-c11">(C11)</span></span></span></div></div></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="cast.html" title="c/language/cast">转型运算符</a></td></tr>
</table></div>
</div><div></div></div></div></div><div class="t-navbar-sep"> </div></div>
<p>算术运算符应用标准数学运算符到其操作数。
</p>
<table class="metadata plainlinks ambox mbox-small-left ambox-notice" style=""><tr><td class="mbox-empty-cell"></td><td class="mbox-text" style="">本节未完成<br>原因：为这个和其他表格考虑更通用的，覆盖多个专题的 ToC </td></tr></table>
<table class="wikitable" style="font-size:85%;">

<tr style="text-align:center">
<th> 运算符
</th>
<th> 运算符名
</th>
<th> 示例
</th>
<th> 结果
</th></tr>
<tr style="text-align:center">
<td> <span class="t-c"><span class="mw-geshi c source-c"><span class="sy2">+</span></span></span>
</td>
<td> 一元加
</td>
<td> <span class="t-c"><span class="mw-geshi c source-c"><span class="sy2">+</span>a</span></span>
</td>
<td> <b>a</b> 提升后的值
</td></tr>
<tr style="text-align:center">
<td> <span class="t-c"><span class="mw-geshi c source-c"><span class="sy2">-</span></span></span>
</td>
<td> 一元减
</td>
<td> <span class="t-c"><span class="mw-geshi c source-c"><span class="sy2">-</span>a</span></span>
</td>
<td> <b>a</b> 的相反数
</td></tr>
<tr style="text-align:center">
<td> <span class="t-c"><span class="mw-geshi c source-c"><span class="sy2">+</span></span></span>
</td>
<td> 加法
</td>
<td> <span class="t-c"><span class="mw-geshi c source-c">a <span class="sy2">+</span> b</span></span>
</td>
<td> <b>a</b> 加 <b>b</b>
</td></tr>
<tr style="text-align:center">
<td> <span class="t-c"><span class="mw-geshi c source-c"><span class="sy2">-</span></span></span>
</td>
<td> 减法
</td>
<td> <span class="t-c"><span class="mw-geshi c source-c">a <span class="sy2">-</span> b</span></span>
</td>
<td> 从 <b>a</b> 减去 <b>b</b>
</td></tr>
<tr style="text-align:center">
<td> <span class="t-c"><span class="mw-geshi c source-c"><span class="sy2">*</span></span></span>
</td>
<td> 乘法
</td>
<td> <span class="t-c"><span class="mw-geshi c source-c">a <span class="sy2">*</span> b</span></span>
</td>
<td> <b>a</b> 和 <b>b</b> 的积
</td></tr>
<tr style="text-align:center">
<td> <span class="t-c"><span class="mw-geshi c source-c"><span class="sy2">/</span></span></span>
</td>
<td> 除法
</td>
<td> <span class="t-c"><span class="mw-geshi c source-c">a <span class="sy2">/</span> b</span></span>
</td>
<td> <b>a</b> 除以 <b>b</b> 的商
</td></tr>
<tr style="text-align:center">
<td> <span class="t-c"><span class="mw-geshi c source-c"><span class="sy2">%</span></span></span>
</td>
<td> 模
</td>
<td> <span class="t-c"><span class="mw-geshi c source-c">a <span class="sy2">%</span> b</span></span>
</td>
<td> <b>a</b> 除以 <b>b</b> 的余数
</td></tr>
<tr style="text-align:center">
<td> <span class="t-c"><span class="mw-geshi c source-c">~</span></span>
</td>
<td> 逐位非
</td>
<td> <span class="t-c"><span class="mw-geshi c source-c">~a</span></span>
</td>
<td> <b>a</b> 的逐位非
</td></tr>
<tr style="text-align:center">
<td> <span class="t-c"><span class="mw-geshi c source-c"><span class="sy3">&amp;</span></span></span>
</td>
<td> 逐位与
</td>
<td> <span class="t-c"><span class="mw-geshi c source-c">a <span class="sy3">&amp;</span> b</span></span>
</td>
<td> <b>a</b> 和 <b>b</b> 的逐位与
</td></tr>
<tr style="text-align:center">
<td> <span class="t-c"><span class="mw-geshi c source-c"><span class="sy3">|</span></span></span>
</td>
<td> 逐位或
</td>
<td> <span class="t-c"><span class="mw-geshi c source-c">a <span class="sy3">|</span> b</span></span>
</td>
<td> <b>a</b> 和 <b>b</b> 的逐位或
</td></tr>
<tr style="text-align:center">
<td> <span class="t-c"><span class="mw-geshi c source-c"><span class="sy3">^</span></span></span>
</td>
<td> 逐位异或
</td>
<td> <span class="t-c"><span class="mw-geshi c source-c">a <span class="sy3">^</span> b</span></span>
</td>
<td> <b>a</b> 和 <b>b</b> 的逐位异或
</td></tr>
<tr style="text-align:center">
<td> <span class="t-c"><span class="mw-geshi c source-c"><span class="sy1">&lt;&lt;</span></span></span>
</td>
<td> 逐位左移
</td>
<td> <span class="t-c"><span class="mw-geshi c source-c">a <span class="sy1">&lt;&lt;</span> b</span></span>
</td>
<td> <b>a</b> 左移 <b>b</b> 位
</td></tr>
<tr style="text-align:center">
<td> <span class="t-c"><span class="mw-geshi c source-c"><span class="sy1">&gt;&gt;</span></span></span>
</td>
<td> 逐位右移
</td>
<td> <span class="t-c"><span class="mw-geshi c source-c">a <span class="sy1">&gt;&gt;</span> b</span></span>
</td>
<td> <b>a</b> 右移 <b>b</b> 位
</td></tr></table>
<h3><span class="mw-headline" id=".E6.BA.A2.E5.87.BA">溢出</span></h3>
<p>无符号整数算术始终进行 <span class="texhtml" style="white-space: nowrap;">modulo 2<span class="t-su">n<br></span></span> ，其中 n 是该特定整数中的位数。例如对于 <span class="t-c"><span class="mw-geshi c source-c"><span class="kw4">unsigned</span> <span class="kw4">int</span></span></span> ，加一到 <span class="t-lc"><a href="../types/limits.html" title="c/types/limits">UINT_MAX</a></span> 给出 <span class="t-c"><span class="mw-geshi c source-c">​<span class="nu0">0</span>​</span></span> ，而从 <span class="t-c"><span class="mw-geshi c source-c">​<span class="nu0">0</span>​</span></span> 减一给出 <span class="t-lc"><a href="../types/limits.html" title="c/types/limits">UINT_MAX</a></span> 。
</p><p>有符号算术运算溢出（结果不符合结果类型）时，行为未定义：可以按照表示（典型为补码）的规则回卷，可以在某些平台上或由于编译器选项（例如 gcc 和 clang 中的 <code>-ftrapv</code> ）陷落，或完全<a rel="nofollow" class="external text" href="http://blog.llvm.org/2011/05/what-every-c-programmer-should-know_14.html">被编译器优化掉</a>。
</p>
<h4><span class="mw-headline" id=".E6.B5.AE.E7.82.B9.E7.8E.AF.E5.A2.83">浮点环境</span></h4>
<p>若设置 <a href="../preprocessor/impl.html" title="c/preprocessor/impl"><tt>
#pragma STDC FENV_ACCESS</tt></a> 为 <code>ON</code> ，则所有浮点算术运算符遵循当前浮点<a href="../numeric/fenv/FE_round.html" title="c/numeric/fenv/FE round">舍入方向</a>并报告 <a href="../numeric/math/math_errhandling.html" title="c/numeric/math/math errhandling"><tt>math_errhandling</tt></a> 中指定的错误，除非它们是<a href="initialization.html" title="c/language/initialization">静态初始化器</a>的一部分（该情况下不引发浮点异常，而舍入模式为到最接近）。
</p>
<h4><span class="mw-headline" id=".E6.B5.AE.E7.82.B9.E7.BC.A9.E7.95.A5">浮点缩略</span></h4>
<p>除非设置 <a href="../preprocessor/impl.html" title="c/preprocessor/impl"><tt>
#pragma STDC FP_CONTRACT</tt></a> 为 <code>OFF</code> ，否则可能如同中间结果拥有无限范围和精度一般进行所有浮点算术，这种优化省略若准确按写法求值表达式，则会观察到的舍入误差和浮点异常。例如，允许以单条融合乘加 CPU 指令实现 <span class="t-c"><span class="mw-geshi c source-c"><span class="br0">(</span>x<span class="sy2">*</span>y<span class="br0">)</span> <span class="sy2">+</span> z</span></span> ，或把 <span class="t-c"><span class="mw-geshi c source-c">a <span class="sy1">=</span> x<span class="sy2">*</span>x<span class="sy2">*</span>x<span class="sy2">*</span>x<span class="sy4">;</span></span></span> 优化成 <span class="t-c"><span class="mw-geshi c source-c">tmp <span class="sy1">=</span> x<span class="sy2">*</span>x<span class="sy4">;</span> a <span class="sy1">=</span> tmp<span class="sy2">*</span>tmp</span></span> 。
</p><p>与缩略无关，浮点算术的中间结果可以拥有异于其类型所指定的范围和精度，见 <span class="t-lc"><a href="../types/limits/FLT_EVAL_METHOD.html" title="c/types/limits/FLT EVAL METHOD">FLT_EVAL_METHOD</a></span> 。
</p>
<h3><span class="mw-headline" id=".E4.B8.80.E5.85.83.E7.AE.97.E6.9C.AF">一元算术</span></h3>
<p>一元算术运算符表达式拥有形式
</p>
<table class="t-sdsc-begin">

<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td> <code><b>+</b></code> <span class="t-spar">expression</span>
</td>
<td> (1)
</td>
<td class="t-sdsc-nopad">
</td></tr>



<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td> <code><b>-</b></code> <span class="t-spar">expression</span>
</td>
<td> (2)
</td>
<td class="t-sdsc-nopad">
</td></tr>



<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr></table>
<div class="t-li1"><span class="t-li">1)</span> 一元加（提升）</div>
<div class="t-li1"><span class="t-li">2)</span> 一元减（相反数）
<p>where 
</p>
<table class="t-par-begin">


<tr class="t-par">
<td> <span class="t-spar">expression</span>
</td>
<td> -
</td>
<td> 任何<a href="arithmetic_types.html" title="c/language/arithmetic types">算术类型</a>表达式
</td></tr></table></div>
<p>一元加和一元减都首先应用<a href="conversion.html" title="c/language/conversion">整数提升</a>到其操作数，然后
</p>
<ul><li> 一元加返回提升后的值
</li><li> 一元减返回提升后值的相反数（除了 NaN 的相反数是另一 NaN ）
</li></ul>
<p>表达式类型为提升后的类型，而<a href="value_category.html" title="c/language/value category">值类别</a>为非左值。
</p>
<h4><span class="mw-headline" id=".E6.B3.A8.E6.84.8F">注意</span></h4>
<p>在典型（补码）平台上应用到 <span class="t-lc"><a href="../types/limits.html" title="c/types/limits">INT_MIN</a></span> 、 <span class="t-lc"><a href="../types/limits.html" title="c/types/limits">LONG_MIN</a></span> 或 <span class="t-lc"><a href="../types/limits.html" title="c/types/limits">LLONG_MIN</a></span> 时，一元减引起有符号整数溢出所致的未定义行为。
</p><p>C++ 中一元运算符 + 亦能用于其他内建类型，例如数组和函数，但 C 中不能。
</p>
<div class="t-example"><div class="t-example-live-link"><div class="coliru-btn coliru-btn-run-init">运行此代码</div></div>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="c source-c"><pre class="de1"><span class="co2">#include &lt;stdio.h&gt;</span>
<span class="co2">#include &lt;complex.h&gt;</span>
<span class="kw4">int</span> main<span class="br0">(</span><span class="kw4">void</span><span class="br0">)</span>
<span class="br0">{</span>
    <span class="kw4">char</span> c <span class="sy1">=</span> <span class="st0">'a'</span><span class="sy4">;</span>
    <a href="../io/fprintf.html"><span class="kw851">printf</span></a><span class="br0">(</span><span class="st0">"sizeof char: %zu sizeof int: %zu<span class="es1">\n</span>"</span>, <span class="kw4">sizeof</span> c, <span class="kw4">sizeof</span> <span class="sy2">+</span>c<span class="br0">)</span><span class="sy4">;</span>
 
    <a href="../io/fprintf.html"><span class="kw851">printf</span></a><span class="br0">(</span><span class="st0">"-1, where 1 is signed: %d<span class="es1">\n</span>"</span>, <span class="sy2">-</span><span class="nu0">1</span><span class="br0">)</span><span class="sy4">;</span>
    <a href="../io/fprintf.html"><span class="kw851">printf</span></a><span class="br0">(</span><span class="st0">"-1, where 1 is unsigned: %u<span class="es1">\n</span>"</span>, <span class="sy2">-</span>1u<span class="br0">)</span><span class="sy4">;</span>
 
    <span class="kw4">double</span> <a href="../numeric/complex/complex.html"><span class="kw743">complex</span></a> z <span class="sy1">=</span> <span class="nu0">1</span> <span class="sy2">+</span> <span class="nu0">2</span><span class="sy2">*</span>I<span class="sy4">;</span>
    <a href="../io/fprintf.html"><span class="kw851">printf</span></a><span class="br0">(</span><span class="st0">"-(1+2i) = %.1f%+.1f<span class="es1">\n</span>"</span>, <a href="../numeric/complex/creal.html"><span class="kw754">creal</span></a><span class="br0">(</span><span class="sy2">-</span>z<span class="br0">)</span>, <a href="../numeric/complex/cimag.html"><span class="kw751">cimag</span></a><span class="br0">(</span><span class="sy2">-</span>z<span class="br0">)</span><span class="br0">)</span><span class="sy4">;</span>
<span class="br0">}</span></pre></div></div>
<p>可能的输出：
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="text source-text"><pre class="de1">sizeof char: 1 sizeof int: 4
-1, where 1 is signed: -1
-1, where 1 is unsigned: 4294967295
-(1+2i) = -1.0-2.0</pre></div></div> 
</div>
<h3><span class="mw-headline" id=".E5.8A.A0.E6.B3.95.E6.80.A7.E8.BF.90.E7.AE.97.E7.AC.A6">加法性运算符</span></h3>
<p>下列加法性算术运算符拥有形式
</p>
<table class="t-sdsc-begin">

<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td> <span class="t-spar">lhs</span> <code><b>+</b></code> <span class="t-spar">rhs</span>
</td>
<td> (1)
</td>
<td class="t-sdsc-nopad">
</td></tr>



<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td> <span class="t-spar">lhs</span> <code><b>-</b></code> <span class="t-spar">rhs</span>
</td>
<td> (2)
</td>
<td class="t-sdsc-nopad">
</td></tr>



<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr></table>
<div class="t-li1"><span class="t-li">1)</span> 加法： <span class="t-spar">lhs</span> 与 <span class="t-spar">rhs</span> 必须为下列之一
<dl><dd><ul><li> 都拥有<a href="arithmetic_types.html" title="c/language/arithmetic types">算术类型</a>，包含复数和虚数
</li><li> 一个是指向完整对象的指针类型，另一个拥有整数类型</li></ul></dd></dl></div>


<div class="t-li1"><span class="t-li">2)</span> 减法： <span class="t-spar">lhs</span> 与 <span class="t-spar">rhs</span> 必须为下列之一
<dl><dd><ul><li> 都拥有<a href="arithmetic_types.html" title="c/language/arithmetic types">算术类型</a>，包含复数和虚数
</li><li> <span class="t-spar">lhs</span> 拥有指向完整对象的指针类型， <span class="t-spar">rhs</span> 拥有整数类型
</li><li> 都是指向拥有<a href="type.html#.E5.85.BC.E5.AE.B9.E7.B1.BB.E5.9E.8B" title="c/language/types">兼容</a>类型的完整对象指针，忽略限定符</li></ul></dd></dl></div>


<h4><span class="mw-headline" id=".E7.AE.97.E6.9C.AF.E5.8A.A0.E6.B3.95.E4.B8.8E.E5.87.8F.E6.B3.95">算术加法与减法</span></h4>
<p>若两个操作数都拥有<a href="arithmetic_types.html" title="c/language/arithmetic types">算术类型</a>，则
</p>
<ul><li> 首先，进行<a href="conversion.html#.E9.80.9A.E5.B8.B8.E7.AE.97.E6.9C.AF.E8.BD.AC.E6.8D.A2" title="c/language/conversion">通常算术转换</a>
</li><li> 然后，遵循数学规则，加或减操作数在提升后的值（对于减法，从 <span class="t-spar">lhs</span> 减去 <span class="t-spar">rhs</span> ），除了
</li></ul>
<dl><dd><ul><li> 若操作数之一为 NaN ，则结果为 NaN
</li><li> 同号无穷大减无穷大为 NaN 并引发 <span class="t-lc"><a href="../numeric/fenv/FE_exceptions.html" title="c/numeric/fenv/FE exceptions">FE_INVALID</a></span>
</li><li> 正无穷大加负无穷大为 NaN 并引发 <span class="t-lc"><a href="../numeric/fenv/FE_exceptions.html" title="c/numeric/fenv/FE exceptions">FE_INVALID</a></span>
</li></ul>
</dd></dl>
<p>复数和虚数加减法定义如下（注意若两个操作数均为虚数则结果类型为虚数，而若一个操作数为实数而另一为虚数，则结果为复数，同通常算术转换所指定）：
</p>
<table class="wikitable" style="text-align:center; font-size:85%; max-width:90em;">

<tr>
<th> + or -
</th>
<th> u
</th>
<th> iv
</th>
<th> u + iv
</th></tr>
<tr>
<th> x
</th>
<td> x ± u
</td>
<td> x ± iv
</td>
<td> (x ± u) ± iv
</td></tr>
<tr>
<th> iy
</th>
<td> ±u + iy
</td>
<td> i(y ± v)
</td>
<td> ±u + i(y ± v)
</td></tr>
<tr>
<th> x + iy
</th>
<td> (x ± u) + iy
</td>
<td> x + i(y ± v)
</td>
<td> (x ± u) + i(y ± v)
</td></tr></table>
<p><br>
</p>
<div class="t-example"><div class="t-example-live-link"><div class="coliru-btn coliru-btn-run-init">运行此代码</div></div>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="c source-c"><pre class="de1"><span class="co1">// 工作中</span>
<span class="co1">// 注意：采用 c/language/conversion 示例的一部分</span></pre></div></div>
<p><br> 
</p>
</div>
<h4><span class="mw-headline" id=".E6.8C.87.E9.92.88.E7.AE.97.E6.9C.AF">指针算术</span></h4>
<ul><li> 若指针 <code>P</code> 指向下标为 <code>I</code> 的数组元素，则
</li></ul>
<dl><dd><ul><li> <span class="t-c"><span class="mw-geshi c source-c">P<span class="sy2">+</span>N</span></span> 与 <span class="t-c"><span class="mw-geshi c source-c">N<span class="sy2">+</span>P</span></span> 是指向同一数组中下标为 <code>I+N</code> 的元素的指针
</li><li> <span class="t-c"><span class="mw-geshi c source-c">P<span class="sy2">-</span>N</span></span> 是指向同一数组中下标为 <code>I-N</code> 的元素的指针
</li></ul>
</dd></dl>
<p>仅若原指针和结果指针都指向同一数组中的元素，或该数组的尾后一位置，行为才有定义。注意在 p 指向数组首元素时，执行 p-1 是未定义行为，并且在某些平台上可能失败。
</p>
<ul><li> 若指针 <code>P1</code> 指向下标为 <code>I</code> 的数组元素（或尾后一位置）而 <code>P2</code> 是指向同一数组的下标为 <code>J</code> 的元素（或尾后一位置），则
</li></ul>
<dl><dd><ul><li> <span class="t-c"><span class="mw-geshi c source-c">P1<span class="sy2">-</span>P2</span></span> 拥有等于 <span class="t-c"><span class="mw-geshi c source-c">I<span class="sy2">-</span>J</span></span> 的值和 <span class="t-lc"><a href="../types/ptrdiff_t.html" title="c/types/ptrdiff t">ptrdiff_t</a></span> 类型（有符号整数类型，最大大小典型地为能声明的最大对象的一半）
</li></ul>
</dd></dl>
<p>仅若结果适合于 <span class="t-lc"><a href="../types/ptrdiff_t.html" title="c/types/ptrdiff t">ptrdiff_t</a></span> ，行为才有定义。
</p><p>为指针算术的目的，把指向非数组元素的对象的指针当做指向大小为 1 的数组首元素的指针。
</p>
<div class="t-example"><div class="t-example-live-link"><div class="coliru-btn coliru-btn-run-init">运行此代码</div></div>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="c source-c"><pre class="de1"><span class="co1">// 工作中</span>
<span class="kw4">int</span> n <span class="sy1">=</span> <span class="nu0">4</span>, m <span class="sy1">=</span> <span class="nu0">3</span><span class="sy4">;</span>
<span class="kw4">int</span> a<span class="br0">[</span>n<span class="br0">]</span><span class="br0">[</span>m<span class="br0">]</span><span class="sy4">;</span>     <span class="co1">// 4 个 VLA 的 VLA ，每个有 3 个 int</span>
<span class="kw4">int</span> <span class="br0">(</span><span class="sy2">*</span>p<span class="br0">)</span><span class="br0">[</span>m<span class="br0">]</span> <span class="sy1">=</span> a<span class="sy4">;</span> <span class="co1">// p == &amp;a[0] </span>
p <span class="sy1">=</span> p <span class="sy2">+</span> <span class="nu0">1</span><span class="sy4">;</span>       <span class="co1">// p == &amp;a[1] （ VLA 所用的指针算术一如平常）</span>
<span class="br0">(</span><span class="sy2">*</span>p<span class="br0">)</span><span class="br0">[</span><span class="nu0">2</span><span class="br0">]</span> <span class="sy1">=</span> <span class="nu0">99</span><span class="sy4">;</span>    <span class="co1">// 更改 a[1][2]</span></pre></div></div>
<p><br> 
</p>
</div>
<h3><span class="mw-headline" id=".E4.B9.98.E6.B3.95.E6.80.A7.E8.BF.90.E7.AE.97.E7.AC.A6">乘法性运算符</span></h3>
<p>下列乘法性运算符拥有形式
</p>
<table class="t-sdsc-begin">

<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td> <span class="t-spar">lhs</span> <code><b>*</b></code> <span class="t-spar">rhs</span>
</td>
<td> (1)
</td>
<td class="t-sdsc-nopad">
</td></tr>



<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td> <span class="t-spar">lhs</span> <code><b>/</b></code> <span class="t-spar">rhs</span>
</td>
<td> (2)
</td>
<td class="t-sdsc-nopad">
</td></tr>



<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td> <span class="t-spar">lhs</span> <code><b>%</b></code> <span class="t-spar">rhs</span>
</td>
<td> (3)
</td>
<td class="t-sdsc-nopad">
</td></tr>



<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr></table>
<div class="t-li1"><span class="t-li">1)</span> 乘法。 <span class="t-spar">lhs</span> 与 <span class="t-spar">rhs</span> 必须拥有<a href="arithmetic_types.html" title="c/language/arithmetic types">算术类型</a></div>
<div class="t-li1"><span class="t-li">2)</span> 除法。 <span class="t-spar">lhs</span> 与 <span class="t-spar">rhs</span> 必须拥有<a href="arithmetic_types.html" title="c/language/arithmetic types">算术类型</a></div>
<div class="t-li1"><span class="t-li">3)</span> 余数。 <span class="t-spar">lhs</span> 与 <span class="t-spar">rhs</span> 必须拥有<a href="arithmetic_types.html" title="c/language/arithmetic types">整数类型</a></div>
<ul><li> 首先，进行<a href="conversion.html#.E9.80.9A.E5.B8.B8.E7.AE.97.E6.9C.AF.E8.BD.AC.E6.8D.A2" title="c/language/conversion">通常算术转换</a>。然后……
</li></ul>
<h4><span class="mw-headline" id=".E4.B9.98.E6.B3.95">乘法</span></h4>
<p>二元运算符 * 遵循通常算术定义，进行其操作数（在通常算术提升后）的乘法，除了
</p>
<ul><li> 若操作数之一为 NaN ，则结果为 NaN
</li><li> 无穷大乘零给出 NaN 并引发 <span class="t-lc"><a href="../numeric/fenv/FE_exceptions.html" title="c/numeric/fenv/FE exceptions">FE_INVALID</a></span>
</li><li> 无穷大乘非零给出无穷大（即使对于复参数）
</li></ul>
<p>因为 C 中，任何带至少一个无穷大部分的<a href="arithmetic_types.html" title="c/language/arithmetic types">复数值</a>为无穷大，即使另一部分为 NaN ，故通常算术规则不应用于复复乘法。其他浮点操作数的组合遵循下表：
</p>
<table class="wikitable" style="text-align:center; font-size:85%; max-width:90em;">

<tr>
<th> *
</th>
<th> u
</th>
<th> iv
</th>
<th> u + iv
</th></tr>
<tr>
<th> x
</th>
<td> xu
</td>
<td> i(xv)
</td>
<td> (xu) + i(xv)
</td></tr>
<tr>
<th> iy
</th>
<td> i(yu)
</td>
<td> −yv
</td>
<td> (−yv) + i(yu)
</td></tr>
<tr>
<th> x + iy
</th>
<td> (xu) + i(yu)
</td>
<td> (−yv) + i(xv)
</td>
<td> <i>special rules</i>
</td></tr></table>
<p>在无穷大处理外，不允许复数乘法上溢中间结果，除了在设置 <a href="../preprocessor/impl.html" title="c/preprocessor/impl"><tt>
#pragma STDC CX_LIMITED_RANGE</tt></a> 为 <code>ON</code> 时，该情况下可以如同 <span class="texhtml" style="white-space: nowrap;">(x+iy)×(u+iv) = (xu-yv)+i(yu+xv)</span> 一般计算值，因为假定了程序员负责限制操作数的范围并处理无穷大。
</p><p>虽然不允许过度的上溢，复数乘法可能引发虚假的浮点异常（否则实现不上溢版本困难到近乎不可能）。
</p>
<div class="t-example"><div class="t-example-live-link"><div class="coliru-btn coliru-btn-run-init">运行此代码</div></div>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="c source-c"><pre class="de1"><span class="co2">#include&lt;stdio.h&gt;</span>
<span class="co2">#include &lt;stdio.h&gt;</span>
<span class="co2">#include &lt;complex.h&gt;</span>
<span class="co2">#include &lt;math.h&gt;</span>
<span class="kw4">int</span> main<span class="br0">(</span><span class="kw4">void</span><span class="br0">)</span>
<span class="br0">{</span>
 
 
<span class="co1">// TODO 从 C++ 采取一些更简单的情况</span>
 
 
   <span class="kw4">double</span> <a href="../numeric/complex/complex.html"><span class="kw743">complex</span></a> z <span class="sy1">=</span> <span class="br0">(</span><span class="nu0">1</span> <span class="sy2">+</span> <span class="nu0">0</span><span class="sy2">*</span>I<span class="br0">)</span> <span class="sy2">*</span> <span class="br0">(</span>INFINITY <span class="sy2">+</span> I<span class="sy2">*</span>INFINITY<span class="br0">)</span><span class="sy4">;</span>
<span class="co1">// 教科书公式会给出</span>
<span class="co1">// (1+i0)(∞+i∞) ⇒ (1×∞ – 0×∞) + i(0×∞+1×∞) ⇒ NaN + I*NaN</span>
<span class="co1">// 但 C 给出复无穷大</span>
   <a href="../io/fprintf.html"><span class="kw851">printf</span></a><span class="br0">(</span><span class="st0">"%f + i*%f<span class="es1">\n</span>"</span>, <a href="../numeric/complex/creal.html"><span class="kw754">creal</span></a><span class="br0">(</span>z<span class="br0">)</span>, <a href="../numeric/complex/cimag.html"><span class="kw751">cimag</span></a><span class="br0">(</span>z<span class="br0">)</span><span class="br0">)</span><span class="sy4">;</span>
 
<span class="co1">// 教科书公式会给出</span>
<span class="co1">// cexp(∞+iNaN) ⇒ exp(∞)×(cis(NaN)) ⇒ NaN + I*NaN</span>
<span class="co1">// 但 C 给出 ±∞+i*nan</span>
   <span class="kw4">double</span> <a href="../numeric/complex/complex.html"><span class="kw743">complex</span></a> y <span class="sy1">=</span> <a href="../numeric/complex/cexp.html"><span class="kw769">cexp</span></a><span class="br0">(</span>INFINITY <span class="sy2">+</span> I<span class="sy2">*</span>NAN<span class="br0">)</span><span class="sy4">;</span>
   <a href="../io/fprintf.html"><span class="kw851">printf</span></a><span class="br0">(</span><span class="st0">"%f + i*%f<span class="es1">\n</span>"</span>, <a href="../numeric/complex/creal.html"><span class="kw754">creal</span></a><span class="br0">(</span>y<span class="br0">)</span>, <a href="../numeric/complex/cimag.html"><span class="kw751">cimag</span></a><span class="br0">(</span>y<span class="br0">)</span><span class="br0">)</span><span class="sy4">;</span>
 
<span class="br0">}</span></pre></div></div>
<p>可能的输出：
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="text source-text"><pre class="de1">inf + i*inf 
inf + i*nan</pre></div></div> 
</div>
<h4><span class="mw-headline" id=".E9.99.A4.E6.B3.95">除法</span></h4>
<p>二元运算符 / 遵循通常的算术定义，将第一操作数除以第二操作数（在通常算术转换后），除了
</p>
<ul><li> 通常算术提升后的类型为整数类型时，结果为代数商（非分数），<span class="t-rev-inl t-until-c99"><span>以实现定义方向取整</span> <span><span class="t-mark-rev t-until-c99">(C99 前)</span></span></span><span class="t-rev-inl t-since-c99"><span>向零截断</span> <span><span class="t-mark-rev t-since-c99">(C99 起)</span></span></span>
</li><li> 若操作数之一为 NaN ，则结果为 NaN
</li><li> 若第一操作数为复无穷大，而第二操作数为有限，则 / 运算符的结果为复无穷大
</li><li> 若第一操作数为有限而第二操作数为复无穷大，则 / 运算符的结果为零
</li></ul>
<p>因为 C 中，任何带至少一个无穷大部分的<a href="arithmetic_types.html" title="c/language/arithmetic types">复数值</a>为无穷大，即使另一部分为 NaN ，故通常算术规则不应用于复复除法。其他浮点操作数的组合遵循下表：
</p>
<table class="wikitable" style="text-align:center; font-size:85%; max-width:90em;">

<tr>
<th> /
</th>
<th> u
</th>
<th> iv
</th></tr>
<tr>
<th> x
</th>
<td> x/u
</td>
<td> i(−x/v)
</td></tr>
<tr>
<th> iy
</th>
<td> i(y/u)
</td>
<td> y/v
</td></tr>
<tr>
<th> x + iy
</th>
<td> (x/u) + i(y/u)
</td>
<td> (y/v) + i(−x/v)
</td></tr></table>
<p>在无穷大处理外，不允许复数除法上溢中间结果，除了在设置 <a href="../preprocessor/impl.html" title="c/preprocessor/impl"><tt>
#pragma STDC CX_LIMITED_RANGE</tt></a> 为 <code>ON</code> 时，该情况下可以如同 <span class="texhtml" style="white-space: nowrap;">(x+iy)/(u+iv) = [(xu+yv)+i(yu-xv)]/(u<span class="t-su">2<br></span>+v<span class="t-su">2<br></span>)</span> 一般计算值，因为假定了程序员负责限制操作数的范围并处理无穷大。
</p><p>虽然不允许过度的上溢，复数除法可能引发虚假的浮点异常（否则实现不上溢版本困难到近乎不可能）。
</p><p>若第二操作数为零，则行为未定义，除非支持 IEE 浮点算术，且发生浮点除法，则
</p>
<ul><li> 非零数除以 ±0.0 给出符号正确的无穷大并引发 <span class="t-lc"><a href="../numeric/fenv/FE_exceptions.html" title="c/numeric/fenv/FE exceptions">FE_DIVBYZERO</a></span>
</li><li> 0.0 除以 0.0 给出 NaN 并引发 <span class="t-lc"><a href="../numeric/fenv/FE_exceptions.html" title="c/numeric/fenv/FE exceptions">FE_INVALID</a></span>
</li></ul>
<h4><span class="mw-headline" id=".E4.BD.99.E6.95.B0">余数</span></h4>
<p>二元运算符 % 产出第一操作数除以第二操作数（在通常算术转换后）的余数。
</p><p>余数的符号定义，使得若商 <code>a/b</code> 能以结果类型表示，则 <span class="t-c"><span class="mw-geshi c source-c"><span class="br0">(</span>a<span class="sy2">/</span>b<span class="br0">)</span><span class="sy2">*</span>b <span class="sy2">+</span> a<span class="sy2">%</span>b <span class="sy1">==</span> a</span></span> 。
</p><p>若第二操作数为零，则行为未定义。
</p><p>若商 <code>a/b</code> 不能以结果类型表示，则 <code>a/b</code> 和 <code>a%b</code> 的行为都未定义（这表明 <span class="t-c"><span class="mw-geshi c source-c">INT_MIN<span class="sy2">%-</span><span class="nu0">1</span></span></span> 在补码系统上未定义）。
</p><p>注意：余数运算符不可用于浮点类型上，库函数 <span class="t-lc"><a href="../numeric/math/fmod.html" title="c/numeric/math/fmod">fmod</a></span> 提供该功能。
</p>
<h3><span class="mw-headline" id=".E9.80.90.E4.BD.8D.E9.80.BB.E8.BE.91">逐位逻辑</span></h3>
<p>逐位逻辑运算符拥有形式
</p>
<table class="t-sdsc-begin">

<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td> <code><b>~</b></code> <span class="t-spar">rhs</span>
</td>
<td> (1)
</td>
<td class="t-sdsc-nopad">
</td></tr>



<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td> <span class="t-spar">lhs</span> <code><b>&amp;</b></code> <span class="t-spar">rhs</span>
</td>
<td> (2)
</td>
<td class="t-sdsc-nopad">
</td></tr>



<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td> <span class="t-spar">lhs</span> <code><b>|</b></code> <span class="t-spar">rhs</span>
</td>
<td> (3)
</td>
<td class="t-sdsc-nopad">
</td></tr>



<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td> <span class="t-spar">lhs</span> <code><b>^</b></code> <span class="t-spar">rhs</span>
</td>
<td> (4)
</td>
<td class="t-sdsc-nopad">
</td></tr>



<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr></table>
<div class="t-li1"><span class="t-li">1)</span> 逐位非</div>
<div class="t-li1"><span class="t-li">2)</span> 逐位与</div>
<div class="t-li1"><span class="t-li">3)</span> 逐位或</div>
<div class="t-li1"><span class="t-li">4)</span> 逐位异或
<p>其中
</p>
<table class="t-par-begin">


<tr class="t-par">
<td>  <span class="t-spar">lhs</span>, <span class="t-spar">rhs</span>
</td>
<td> -
</td>
<td> 整数类型表达式
</td></tr></table></div>
<p>首先，运算符 &amp; 、 ^ 和 | 在两个操作数上进行<a href="conversion.html#.E9.80.9A.E5.B8.B8.E7.AE.97.E6.9C.AF.E8.BD.AC.E6.8D.A2" title="c/language/conversion">通常算术转换</a>，而运算符 ~ 在其唯一的操作数上进行<a href="conversion.html#.E6.95.B4.E6.95.B0.E6.8F.90.E5.8D.87" title="c/language/conversion">整数提升</a>。
</p><p>然后，逐位应用对应的逻辑运算符；即按照应用到操作数的每位的逻辑运算（或、与、非、异或），设置或清除结果的对应位。
</p><p>注意：位运算符常用于操作位集和位掩码。
</p><p>注意：对于无符号类型（提升后），表达式 ~E 等价于结果类型的最大可表示值减 E 的原值。
</p>
<div class="t-example"><div class="t-example-live-link"><div class="coliru-btn coliru-btn-run-init">运行此代码</div></div>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="c source-c"><pre class="de1"><span class="co2">#include &lt;stdio.h&gt;</span>
<span class="co2">#include &lt;stdint.h&gt;</span>
<span class="kw4">int</span> main<span class="br0">(</span><span class="kw4">void</span><span class="br0">)</span>
<span class="br0">{</span>
    <a href="../types/integer.html"><span class="kw121">uint16_t</span></a> mask <span class="sy1">=</span> <span class="nu12">0x00f0</span><span class="sy4">;</span>
    <a href="../types/integer.html"><span class="kw122">uint32_t</span></a> a <span class="sy1">=</span> <span class="nu12">0x12345678</span><span class="sy4">;</span>
    <a href="../io/fprintf.html"><span class="kw851">printf</span></a><span class="br0">(</span><span class="st0">"Value: %#x mask: %#x<span class="es1">\n</span>"</span>
           <span class="st0">"Setting bits:   %#x<span class="es1">\n</span>"</span>
           <span class="st0">"Clearing bits:  %#x<span class="es1">\n</span>"</span>
           <span class="st0">"Selecting bits: %#x<span class="es1">\n</span>"</span>,
           a,mask,<span class="br0">(</span>a<span class="sy3">|</span>mask<span class="br0">)</span>,<span class="br0">(</span>a<span class="sy3">&amp;</span>~mask<span class="br0">)</span>,<span class="br0">(</span>a<span class="sy3">&amp;</span>mask<span class="br0">)</span><span class="br0">)</span><span class="sy4">;</span>
<span class="br0">}</span></pre></div></div>
<p>可能的输出：
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="text source-text"><pre class="de1">Value: 0x12345678 mask: 0xf0
Setting bits:   0x123456f8
Clearing bits:  0x12345608
Selecting bits: 0x70</pre></div></div> 
</div>
<h3><span class="mw-headline" id=".E4.BD.8D.E7.A7.BB.E8.BF.90.E7.AE.97.E7.AC.A6">位移运算符</span></h3>
<p>位移运算符表达式拥有形式
</p>
<table class="t-sdsc-begin">

<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td> <span class="t-spar">lhs</span> <code><b>&lt;&lt;</b></code> <span class="t-spar">rhs</span>
</td>
<td> (1)
</td>
<td class="t-sdsc-nopad">
</td></tr>



<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td> <span class="t-spar">lhs</span> <code><b>&gt;&gt;</b></code> <span class="t-spar">rhs</span>
</td>
<td> (2)
</td>
<td class="t-sdsc-nopad">
</td></tr>



<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr></table>
<div class="t-li1"><span class="t-li">1)</span> <span class="t-spar">lhs</span> 左移 <span class="t-spar">rhs</span> 位</div>
<div class="t-li1"><span class="t-li">2)</span> <span class="t-spar">lhs</span> 右移 <span class="t-spar">rhs</span> 位
<p>其中
</p>
<table class="t-par-begin">


<tr class="t-par">
<td>  <span class="t-spar">lhs</span>, <span class="t-spar">rhs</span>
</td>
<td> -
</td>
<td> 整数类型表达式
</td></tr></table></div>
<p>首先，在每个操作数上独自进行<a href="conversion.html" title="c/language/conversion">整数提升</a>（注意：这不同于其他二元算术运算符，它们全都进行通常算术转换）。结果类型为 <span class="t-spar">lhs</span> 在提升后的类型。
</p><p>对于无符号 <span class="t-spar">lhs</span> ， <code>LHS &lt;&lt; RHS</code> 的值是 <span class="texhtml" style="white-space: nowrap;">LHS * 2<span class="t-su">RHS<br></span></span> 以返回类型最大值加 1 取模（即进行逐位左移，并舍弃移出目标类型的位）。对于有非负值的有符号 <span class="t-spar">lhs</span> ，若值能以 <span class="t-spar">lhs</span> 的提升后类型表示，则 <code>LHS &lt;&lt; RHS</code> 的值是 <span class="texhtml" style="white-space: nowrap;">LHS * 2<span class="t-su">RHS<br></span></span> ，否则行为未定义。
</p><p>对于无符号 <span class="t-spar">lhs</span> 和有非负值的有符号 <span class="t-spar">lhs</span> ， <code>LHS &gt;&gt; RHS</code> 的值是 <span class="texhtml" style="white-space: nowrap;">LHS / 2<span class="t-su">RHS<br></span></span> 的整数部分。对于负的 <code>LHS</code> ， <code>LHS &gt;&gt; RHS</code> 的值是实现定义的，大多数实现中，这进行算术右移（故结果仍为负）。从而在大多数实现中，右移有符号的 <code>LHS</code> 会以原符号位填充新的高位（即若原数非负则为 0 ，而若原数为负则为 1 ）。
</p><p>任何情况下，若 <span class="t-spar">rhs</span> 为负或大于或等于提升后的 <span class="t-spar">lhs</span> 中的位数，则行为未定义。
</p>
<div class="t-example"><div class="t-example-live-link"><div class="coliru-btn coliru-btn-run-init">运行此代码</div></div>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="c source-c"><pre class="de1"><span class="co2">#include &lt;stdio.h&gt;</span>
<span class="kw2">enum</span> <span class="br0">{</span>ONE<span class="sy1">=</span><span class="nu0">1</span>, TWO<span class="sy1">=</span><span class="nu0">2</span><span class="br0">}</span><span class="sy4">;</span>
<span class="kw4">int</span> main<span class="br0">(</span><span class="kw4">void</span><span class="br0">)</span>
<span class="br0">{</span>
    <span class="kw4">char</span> c <span class="sy1">=</span> <span class="nu12">0x10</span><span class="sy4">;</span>
    <span class="kw4">unsigned</span> <span class="kw4">long</span> <span class="kw4">long</span> ulong_num <span class="sy1">=</span> <span class="nu12">0x123</span><span class="sy4">;</span>
    <a href="../io/fprintf.html"><span class="kw851">printf</span></a><span class="br0">(</span><span class="st0">"0x123 &lt;&lt; 1  = %#llx<span class="es1">\n</span>"</span>
           <span class="st0">"0x123 &lt;&lt; 63 = %#llx<span class="es1">\n</span>"</span>   <span class="co1">// 对无符号数，溢出截断高位</span>
           <span class="st0">"0x10  &lt;&lt; 10 = %#x<span class="es1">\n</span>"</span>,    <span class="co1">// char 被提升到 int</span>
           ulong_num <span class="sy1">&lt;&lt;</span> <span class="nu0">1</span>, ulong_num <span class="sy1">&lt;&lt;</span> <span class="nu0">63</span>, c <span class="sy1">&lt;&lt;</span> <span class="nu0">10</span><span class="br0">)</span><span class="sy4">;</span>
    <span class="kw4">long</span> <span class="kw4">long</span> long_num <span class="sy1">=</span> <span class="sy2">-</span><span class="nu0">1000</span><span class="sy4">;</span>
    <a href="../io/fprintf.html"><span class="kw851">printf</span></a><span class="br0">(</span><span class="st0">"-1000 &gt;&gt; 1 = %lld<span class="es1">\n</span>"</span>, long_num <span class="sy1">&gt;&gt;</span> ONE<span class="br0">)</span><span class="sy4">;</span>  <span class="co1">// 实现定义</span>
<span class="br0">}</span></pre></div></div>
<p>可能的输出：
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="text source-text"><pre class="de1">0x123 &lt;&lt; 1  = 0x246
0x123 &lt;&lt; 63 = 0x8000000000000000
0x10  &lt;&lt; 10 = 0x4000
-1000 &gt;&gt; 1 = -500</pre></div></div> 
</div>
<h3><span class="mw-headline" id=".E5.BC.95.E7.94.A8">引用</span></h3>
<div class="t-ref-std-11">
<ul><li> C11 standard (ISO/IEC 9899:2011): 
</li></ul>
<dl><dd><ul><li> 6.5.3.3 Unary arithmetic operators (p: 89)
</li></ul>
</dd></dl>
<dl><dd><ul><li> 6.5.5 Multiplicative operators (p: 92)
</li></ul>
</dd></dl>
<dl><dd><ul><li> 6.5.6 Additive operators (p: 92-94)
</li></ul>
</dd></dl>
<dl><dd><ul><li> 6.5.7 Bitwise shift operators (p: 94-95)
</li></ul>
</dd></dl>
<dl><dd><ul><li> 6.5.10 Bitwise AND operator (p: 97)
</li></ul>
</dd></dl>
<dl><dd><ul><li> 6.5.11 Bitwise exclusive OR operator (p: 98)
</li></ul>
</dd></dl>
<dl><dd><ul><li> 6.5.12 Bitwise inclusive OR operator (p: 98)
</li></ul>
</dd></dl>
<div class="t-ref-std-c99">
<ul><li> C99 standard (ISO/IEC 9899:1999): 
</li></ul>
<dl><dd><ul><li> 6.5.3.3 Unary arithmetic operators (p: 79)
</li></ul>
</dd></dl>
<dl><dd><ul><li> 6.5.5 Multiplicative operators (p: 82)
</li></ul>
</dd></dl>
<dl><dd><ul><li> 6.5.6 Additive operators (p: 82-84)
</li></ul>
</dd></dl>
<dl><dd><ul><li> 6.5.7 Bitwise shift operators (p: 84-85)
</li></ul>
</dd></dl>
<dl><dd><ul><li> 6.5.10 Bitwise AND operator (p: 87)
</li></ul>
</dd></dl>
<dl><dd><ul><li> 6.5.11 Bitwise exclusive OR operator (p: 88)
</li></ul>
</dd></dl>
<dl><dd><ul><li> 6.5.12 Bitwise inclusive OR operator (p: 88)
</li></ul>
</dd></dl>
<div class="t-ref-std-c89">
<ul><li> C89/C90 standard (ISO/IEC 9899:1990): 
</li></ul>
<dl><dd><ul><li> 3.3.3.3 Unary arithmetic operators 
</li></ul>
</dd></dl>
<dl><dd><ul><li> 3.3.5 Multiplicative operators 
</li></ul>
</dd></dl>
<dl><dd><ul><li> 3.3.6 Additive operators 
</li></ul>
</dd></dl>
<dl><dd><ul><li> 3.3.7 Bitwise shift operators 
</li></ul>
</dd></dl>
<dl><dd><ul><li> 3.3.10 Bitwise AND operator 
</li></ul>
</dd></dl>
<dl><dd><ul><li> 3.3.11 Bitwise exclusive OR operator 
</li></ul>
</dd></dl>
<dl><dd><ul><li> 3.3.12 Bitwise inclusive OR operator 
</li></ul>
</dd></dl>
</div>
<h3><span class="mw-headline" id=".E5.8F.82.E9.98.85">参阅</span></h3>
<p><a href="operator_precedence.html" title="c/language/operator precedence">运算符优先级</a>
</p>
<table class="wikitable">

<tr style="text-align:center">
<th colspan="7"> 常用运算符
</th></tr>
<tr style="text-align:center">
<td> <a href="operator_assignment.html" title="c/language/operator assignment">赋值</a>
</td>
<td> <a href="operator_incdec.html" title="c/language/operator incdec">自增<br>自减</a>
</td>
<td> <strong class="selflink">算术</strong>
</td>
<td> <a href="operator_logical.html" title="c/language/operator logical">逻辑</a>
</td>
<td> <a href="operator_comparison.html" title="c/language/operator comparison">比较</a>
</td>
<td> <a href="operator_member_access.html" title="c/language/operator member access">成员<br>访问</a>
</td>
<td> <a href="operator_other.html" title="c/language/operator other">其他</a>
</td></tr>
<tr style="text-align:center">
<td>
<p><span class="t-c"><span class="mw-geshi c source-c">a <span class="sy1">=</span> b<br>
a <span class="sy2">+</span><span class="sy1">=</span> b<br>
a <span class="sy2">-</span><span class="sy1">=</span> b<br>
a <span class="sy2">*</span><span class="sy1">=</span> b<br>
a <span class="sy2">/</span><span class="sy1">=</span> b<br>
a <span class="sy2">%</span><span class="sy1">=</span> b<br>
a <span class="sy3">&amp;</span><span class="sy1">=</span> b<br>
a <span class="sy3">|</span><span class="sy1">=</span> b<br>
a <span class="sy3">^</span><span class="sy1">=</span> b<br>
a <span class="sy1">&lt;&lt;=</span> b<br>
a <span class="sy1">&gt;&gt;=</span> b</span></span>
</p>
</td>
<td>
<p><span class="t-c"><span class="mw-geshi c source-c"><span class="sy2">++</span>a<br>
<span class="sy2">--</span>a<br>
a<span class="sy2">++</span><br>
a<span class="sy2">--</span></span></span>
</p>
</td>
<td>
<p><span class="t-c"><span class="mw-geshi c source-c"><span class="sy2">+</span>a<br>
<span class="sy2">-</span>a<br>
a <span class="sy2">+</span> b<br>
a <span class="sy2">-</span> b<br>
a <span class="sy2">*</span> b<br>
a <span class="sy2">/</span> b<br>
a <span class="sy2">%</span> b<br>
~a<br>
a <span class="sy3">&amp;</span> b<br>
a <span class="sy3">|</span> b<br>
a <span class="sy3">^</span> b<br>
a <span class="sy1">&lt;&lt;</span> b<br>
a <span class="sy1">&gt;&gt;</span> b</span></span>
</p>
</td>
<td>
<p><span class="t-c"><span class="mw-geshi c source-c"><span class="sy3">!</span>a<br>
a <span class="sy3">&amp;&amp;</span> b<br>
a <span class="sy3">||</span> b</span></span>
</p>
</td>
<td>
<p><span class="t-c"><span class="mw-geshi c source-c">a <span class="sy1">==</span> b<br>
a <span class="sy3">!</span><span class="sy1">=</span> b<br>
a <span class="sy1">&lt;</span> b<br>
a <span class="sy1">&gt;</span> b<br>
a <span class="sy1">&lt;=</span> b<br>
a <span class="sy1">&gt;=</span> b</span></span>
</p>
</td>
<td>
<p><span class="t-c"><span class="mw-geshi c source-c">a<span class="br0">[</span>b<span class="br0">]</span><br>
<span class="sy2">*</span>a<br>
<span class="sy3">&amp;</span>a<br>
a<span class="sy2">-</span><span class="sy1">&gt;</span>b<br>
a.<span class="me1">b</span></span></span>
</p>
</td>
<td>
<p><span class="t-c"><span class="mw-geshi c source-c">a<span class="br0">(</span>...<span class="br0">)</span><br>
a, b<br>
<span class="br0">(</span>type<span class="br0">)</span> a<br>
<span class="sy4">?</span> <span class="sy4">:</span><br>
<span class="kw4">sizeof</span><br>
_Alignof</span></span> <span class="t-mark-rev t-since-c11">(C11 起)</span>
</p>
</td></tr>
</table>
<table class="t-dsc-begin">

</table></div>
</div>

<!-- 
NewPP limit report
Preprocessor visited node count: 5149/1000000
Preprocessor generated node count: 13654/1000000
Post‐expand include size: 67349/2097152 bytes
Template argument size: 18014/2097152 bytes
Highest expansion depth: 17/40
Expensive parser function count: 0/100
-->

<!-- Saved in parser cache with key mwiki1-mwiki_zh_:pcache:idhash:7985-0!*!0!!zh!*!zh!* and timestamp 20200217123846 -->
</div>                    <!-- /bodycontent -->
                                        <!-- printfooter -->
                    <div class="printfooter">
                    来自“<a href="https://zh.cppreference.com/mwiki/index.php?title=c/language/operator_arithmetic&amp;oldid=63501">https://zh.cppreference.com/mwiki/index.php?title=c/language/operator_arithmetic&amp;oldid=63501</a>”                    </div>
                    <!-- /printfooter -->
                                                            <!-- catlinks -->
                    <!-- /catlinks -->
                                                            <div class="visualClear"></div>
                    <!-- debughtml -->
                                        <!-- /debughtml -->
                </div>
                <!-- /bodyContent -->
            </div>
        </div>
        <!-- /content -->
        <!-- footer -->
        <div id="cpp-footer-base" class="noprint">
            <div id="footer">
                        <div id="cpp-navigation">
            <h5>导航</h5>
            <ul><li><a href="https://zh.cppreference.com/w/c/language/operator_arithmetic">Online version</a></li><li>Offline version retrieved 2020-03-14 18:00.</li></ul></div>
                        <ul id="footer-info">
                                    <li id="footer-info-lastmod"> 本页面最后修改于2019年12月31日 (星期二) 22:23。</li>
                                    <li id="footer-info-viewcount">此页面已被浏览过2,623次。</li>
                            </ul>
                    </div>
        </div>
        <!-- /footer -->
        <script>if(window.mw){
mw.loader.state({"site":"loading","user":"missing","user.groups":"ready"});
}</script>
<script src="../../../common/skin_scripts.js"></script>
<script>if(window.mw){
mw.loader.load(["mediawiki.action.view.postEdit","mediawiki.user","mediawiki.page.ready","mediawiki.searchSuggest","mediawiki.hidpi","ext.gadget.MathJax","ext.gadget.ColiruCompiler"], null, true);
}</script>
<script src="../../../common/site_scripts.js"></script>
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-2828341-1']);
_gaq.push(['_setDomainName', 'cppreference.com']);
_gaq.push(['_trackPageview']);
</script><!-- Served in 1.998 secs. -->
	</body>
<!-- Cached 20200217123846 -->
</html>